// 类型判断工具
const types = {
  string: '[object String]',
  regexp: '[object RegExp]',
  number: '[object Number]',
  boolean: '[object Boolean]',
  object: '[object Object]',
  array: '[object Array]',
  function: '[object Function]',
  symbol: '[object Symbol]',
  undefined: '[object Undefined]',
  null: '[object Null]',
  map: '[object Map]',
  weakMap: ' [object WeakMap]',
  set: '[object Set]',
  weakSet: '[object WeakSet]',
  promise: '[object Promise]',
};

const getType = (value, type) => {
  return Object.prototype.toString.call(value) === types[type];
};

const isFalse = (v) => {
  if (['', null, undefined, 'null', 'undefined', 0, '0', false, 'false', NaN, 'NaN'].includes(v)) {
    return true;
  }
  return false;
};

export default {
  isFalse,
  isTrue(v) {
    return !isFalse(v);
  },
  isString: (v) => getType(v, 'string'),
  isRegexp: (v) => getType(v, 'regexp'),
  isNumber: (v) => getType(v, 'number'),
  isBoolean: (v) => getType(v, 'boolean'),
  isObject: (v) => getType(v, 'object'),
  isArray: (v) => getType(v, 'array'),
  isFunction: (v) => getType(v, 'function'),
  isSymbol: (v) => getType(v, 'symbol'),
  isUndefined: (v) => getType(v, 'undefined'),
  isPromise: (v) => getType(v, 'promise'),
  isMap: (v) => getType(v, 'map'),
  isWeakMap: (v) => getType(v, 'weakMap'),
  isSet: (v) => getType(v, 'set'),
  isWeakSet: (v) => getType(v, 'weakSet'),
  isNull: (v) => v == null,
  isEmpty: (v) => {
    if (v == null) {
      return true;
    }
    if (getType(v, 'number')) {
      return !v;
    }
    if (getType(v, 'boolean')) {
      return false;
    }
    if (getType(v, 'string') || getType(v, 'array')) {
      return !v.length;
    }
    if (getType(v, 'map') || getType(v, 'weakMap') || getType(v, 'set') || getType(v, 'weakSet')) {
      return !v.size;
    }
    if (getType(v, 'object')) {
      return !Object.keys(v).length;
    }
  },
};
